/**
 * 
 */
package mx.netbrain.bean;

import java.io.Serializable;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import mx.netbrain.controller.TypeFileController;
import mx.netbrain.dao.TypeFileDao;
import mx.netbrain.dao.impl.TypeFileDaoImpl;
import mx.netbrain.model.Type;

import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;

/**
 * @author julian
 * 
 */
public class TypeFileBean implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 863881942488766928L;
	private UploadedFile file;
	private Type type;
	private List<Type> listTypeFile;

	public TypeFileBean() {
	}

	public void prepareFile(Long id) {
		TypeFileDao typeFileDao = new TypeFileDaoImpl();
		type = typeFileDao.findById(id);
	}

	public void delete() {
		TypeFileDao typeFileDao = new TypeFileDaoImpl();
		typeFileDao.delete(type);
		FacesContext context = FacesContext.getCurrentInstance();
		context.addMessage(null, new FacesMessage(
				"El tipo se borro correctamente!", "Borrado"));
		type = new Type();
	}

	public void handleFileUpload(FileUploadEvent event) {
		FacesMessage msg = new FacesMessage("Succesful", event.getFile()
				.getFileName() + " is uploaded.");
		FacesContext.getCurrentInstance().addMessage(null, msg);
		file = event.getFile();
		System.out.println(file.getFileName());
		TypeFileController.uploadValidate(file);
	}

	public UploadedFile getFile() {
		return file;
	}

	public void setFile(UploadedFile file) {
		this.file = file;
	}

	public Type getType() {
		if (type == null) {
			type = new Type();
		}
		return type;
	}

	public void setType(Type type) {
		this.type = type;
	}

	public List<Type> getListTypeFile() {
		TypeFileDao typeFileDao = new TypeFileDaoImpl();
		listTypeFile = typeFileDao.listAll();
		return listTypeFile;
	}

	public void setListTypeFile(List<Type> listTypeFile) {
		this.listTypeFile = listTypeFile;
	}

}
